import { EnvEnum } from '@app/common/enum/envEnum';
import { ConfigService } from '@nestjs/config';
import { Controller, Get } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { Crud } from 'nestjs-mongoose-crud';
import { User } from '@libs/db/models/user.model';
import { Model } from 'mongoose';
import { InjectModel } from 'nestjs-typegoose';

@Controller('users')
@ApiTags('用户')
@Crud({
  model: User,
})
export class UsersController {
  constructor(
    @InjectModel(User) private readonly model: Model<User>,
    private readonly configService: ConfigService,
  ) {}

  @Get('options')
  getOptions() {
    const uploadUrl =
      this.configService.get('env')[
        process.env.NODE_ENV === 'produciton'
          ? EnvEnum.dev_upload
          : EnvEnum.pro_upload
      ];
    return {
      title: '用户管理',
      columns: [
        { label: 'ID', prop: '_id' },
        { label: '用户名', prop: 'username', search: true },
        { label: '密码', prop: 'password', column: true },
        {
          label: '头像',
          prop: 'avatar',
          upload: true,
          uploadUrl: uploadUrl,
        },
        { label: '个人简介', prop: 'description', column: true },
      ],
    };
  }
}
